using UnityEngine;
using YooAsset;
using IngameDebugConsole;
using System;
using UnityEngine.UI;

namespace LitFramework
{
    public class GameManager : MonoBehaviour
    {
        public static GameManager Instance { get; private set; }
        public string InitState { get; set; }
        public float InitProgress { get; set; }

        public Camera MainCamera;
        public Camera UICamera;

        public ResourceDownloaderOperation downloader;

        private StateMechine stateMechine;
        public DebugLogManager ingameDebug;
        public Button gmButton;

        private void Start()
        {
            Instance = this;
            DontDestroyOnLoad(gameObject);
            stateMechine = new StateMechine();
            var initState = new GameState_Init();
            stateMechine.AddState(initState);

#if UNITY_WECHAT_GAME
            var forceState = new GameState_ForceUpdate_WxMiniGame();
#else
            var forceState = new GameState_ForceUpdate();
#endif
            stateMechine.AddState(forceState);

            var updateState = new GameState_HotUpdate();
            var hyBirdState = new GameState_Hybrid();
            stateMechine.AddState(updateState);
            stateMechine.AddState(hyBirdState);
            
            stateMechine.Start<GameState_Init>();
            ingameDebug.gameObject.SetActive(false);
        }

        private void Update()
        {
            stateMechine.Update();
        }

        public void OpenGM(Action onClick)
        {
            gmButton.onClick.AddListener(() => { onClick?.Invoke(); });
            ingameDebug.gameObject.SetActive(true);
        }

        public void CloseGM()
        {
            ingameDebug.gameObject.SetActive(false);
            gmButton.onClick.RemoveAllListeners();
        }
    }
}